<?php
class Photo_model extends Model {

    var $photo_id = '';
	var $category_id = '';
    var $name = '';
	var $file_name = '';
	var $position = '';
	var $created = '';
	var $changed = '';

    function Photo_model()
    {
        // Call the Model constructor
        parent::Model();
		$this->load->model('Category_model');
    }
    
    function get_all()
    {
		$this->db->order_by('position');
        $query = $this->db->get('photo');
        return $query->result();
    }
	
	function get_all_by_category($category_id){
		$this->db->where('category_id',$category_id);
		$this->db->order_by('position');
        $query = $this->db->get('photo');
        return $query->result();
	}
	
	function get_photo($id)
	{
		$this->db->where('photo_id',$id);
		$query = $this->db->get('photo');
		if( $query->num_rows() > 0){
			$row = $query->row();
			$this->photo_id = $id;
			$this->category_id = $row->category_id;
			$this->name = $row->name;
			$this->file_name = $row->file_name;
			$this->position = $row->position;
			$this->created = $row->created;
			$this->changed = $row->changed;
			return $row;
		}
		else{
			return FALSE;
		}

	}
	
	function create_thumbnail()
	{
		$config['image_library'] = 'gd2';
		$config['source_image'] = './data/photo/'.$this->photo_id.'.jpg';
		$config['create_thumb'] = TRUE;
		$config['maintain_ratio'] = TRUE;
		$config['width'] = 100;
		$config['height'] = 100;
				
		$this->load->library('image_lib');
		$this->image_lib->initialize($config);
		
		if (! $this->image_lib->resize())
		{
			$this->session->set_flashdata('msg', $this->image_lib->display_errors());
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function resize()
	{
		$config['image_library'] = 'gd2';
		$config['source_image'] = './data/photo/'.$this->photo_id.'.jpg';
		$config['new_image'] = './data/photo/'.$this->photo_id.'.jpg';
		$config['maintain_ratio'] = TRUE;
		$config['width'] = 600;
		$config['height'] = 450;
				
		$this->load->library('image_lib', $config);
		
		if (!$this->image_lib->resize())
		{
			$this->session->set_flashdata('msg', $this->image_lib->display_errors());
			return FALSE;
		}
		else{
			return TRUE;
		}
		
	}
	
	function upload()
	{
		$config['upload_path'] = './data/photo/';
		$config['allowed_types'] = 'jpg';
		$config['max_size']	= '2048';
//		$config['max_width']  = '1024';
//		$config['max_height']  = '768';
		$config['encrypt_name']  = TRUE; //change file name to guid
		
		$this->load->library('upload', $config);
		
		if (!$this->upload->do_upload())
		{
			$this->session->set_flashdata('msg', $this->upload->display_errors());
			return FALSE;
		}	
		else
		{
			$data = $this->upload->data();
			$this->photo_id = $data['raw_name'];
			$this->file_name = $data['orig_name'];
			rename($data['full_path'],$data['file_path'].strtolower($data['file_name']));
			
			if(!$this->create_thumbnail()){
				return FALSE;
			}
			if(!$this->resize()){
				return FALSE;
			}
			return TRUE;
		}
	}

    function insert()
    {
        $this->category_id = $this->input->post('category_id');
		$this->name = $this->input->post('name');
		$this->position = $this->Category_model->get_max_position($this->category_id) + 1;
		$this->created = date(SQL_TIMESTAMP_FORMAT);
		$this->changed = date(SQL_TIMESTAMP_FORMAT);
		
		if(trim($this->photo_id) == ''){
			$this->session->set_flashdata('msg', 'id cannot be empty');
			return FALSE;
		}
		
		if(!$this->Category_model->exists($this->category_id)){
			$this->session->set_flashdata('msg', 'category id does not exist');
			return FALSE;
		}
		
	    $this->db->insert('photo', $this);
		$this->session->set_flashdata('msg', $this->db->_error_message());
		return $this->db->trans_status();
		
    }

    function update()
    {
        $this->photo_id = $this->input->post('photo_id');
        $this->category_id = $this->input->post('category_id');
		$this->name = $this->input->post('name');
		$this->changed = date(SQL_TIMESTAMP_FORMAT);
		
		if(!$this->Category_model->exists($this->category_id)){
			$this->session->set_flashdata('msg', 'category id does not exist');
			return FALSE;
		}
		
        $this->db->update('photo', $this, array('photo_id' => $this->input->post('photo_id')));
		return $this->db->trans_status();
    }


	function set_category_id()
	{
        $this->photo_id = $this->input->post('photo_id');
		$this->get_photo($this->photo_id);
		if($this->category_id == $this->input->post('category_id')){
			$this->session->set_flashdata('msg','cannot drop in same category');
			return FALSE;
		}
        $this->category_id = $this->input->post('category_id');
		$this->position = $this->Category_model->get_max_position($this->category_id) + 1;
		$this->changed = date(SQL_TIMESTAMP_FORMAT);
		
		if(!$this->Category_model->exists($this->category_id)){
			$this->session->set_flashdata('msg', 'category id does not exist');
			return FALSE;
		}
		
        $this->db->update('photo', $this, array('photo_id'=>$this->photo_id));
		$this->session->set_flashdata('msg', $this->db->_error_message());
		return $this->db->trans_status();
	}
	
	function get_category_id($id)
	{
		$this->db->from('photo');
		$this->db->where('photo_id',$id);
		$query = $this->db->get('photo');
		if($query->num_rows() > 0){
			$row = $query->row();
			return $row->category_id;
		}
		else{
			return FALSE;
		}
	}
	
	function set_position(){	
		$items = $this->input->post('item');
		$category_id = $this->input->post('category_id');
		$action = $this->input->post('action');
		if( is_array($items) ){
			foreach( $items as $pos=>$item ){
				$this->get_photo($item);
				$this->position = $pos + 1; // 0=1, 1=2...
				$this->category_id = $category_id;
				$this->changed = date(SQL_TIMESTAMP_FORMAT);
				$this->db->update('photo', $this, array('photo_id'=>$this->photo_id));
				
				// if an update query fails: return FALSE
				if(!$this->db->trans_status()){
					$this->session->set_flashdata('msg',$this->db->_error_message());
					return FALSE;
				}
			}
		}
		return TRUE;
	}
	
	
	function delete(){
		$this->photo_id = $this->input->post('photo_id');
		if($this->get_photo($this->photo_id)){
			$this->db->where('id', $this->photo_id);
			$this->db->delete('mytable'); 
			if( $this->db->trans_status() ){
					echo 'ok';
					return TRUE;
			}
			else{
					echo $this->db->_error_message();
					return FALSE;
			}
		}
	}
	
/*	function save_changes(){
		if($this->photo_id != ''){
			$this->db->where('photo_id',$this->photo_id);
			$query = $this->db->get('photo');
			if($query->num_rows()>0){
				$data = $query->row();
				
				if($this->category_id != ''){
					$data['category_id'] = $this->category_id;
					$data['position'] =
						$this->Category_model->get_max_position($this->category_id) + 1;
				}
				$data['changed'] = time();
				$this->db->update('photo', $data, array('photo_id' => $this->photo_id));
			}
		}
	}*/

}
?>